//
// Created by Tan Ke on 2023/12/18.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int cmpFunc(const void* ptr1, const void* ptr2)
{
    int val1 = *((int*)ptr1);
    int val2 = *((int*)ptr2);

    return val1 - val2;
}

long long pickGifts(int* gifts, int giftsSize, int k)
{
    long long result = 0;

    while (k > 0) {
        qsort(gifts, giftsSize, sizeof(int), cmpFunc);
#if 0
        for (int i = 0; i < giftsSize; i ++) {
            printf("%d ", gifts[i]);
        }
        printf("\n");
#endif
        gifts[giftsSize-1] = (int) sqrt(gifts[giftsSize-1]);
        k -= 1;
    }

    for (int i = 0; i < giftsSize; i ++) {
        result += gifts[i];
    }

    return result;
}


int main(void)
{
    int num1[5] = {25, 64, 9, 4, 100};
    printf("%lld\n", pickGifts(num1, 5, 4));

    return 0;
}